package cn.edu.cqut.test0507;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;
import java.util.Scanner;

/*
 * 文本文件的输入/输出（字符流）；二进制文件的输入/输出（字节流）
 * 字符流：XXXReader/XXXWriter-》FileReader/FileWriter；
 * StringReader/StringWriter；BufferedReader/BufferedWriter
 * 字节流：XXXInputStream/XXXOutputStream->FileInputStream/FileOutputStream;
 * BufferedInputStream/BufferedOutputStream;DataInputStream/DataOutputStream;
 * ObjectInputStream/ObjectOutputSteam
 */
public class Test1 {
	public static void main(String[] args) throws Exception {
		File file = new File("test.txt");// 1,098,380 字节
		File file1 = new File("test.dat");// 400,000 字节
		FileWriter fileWriter = new FileWriter(file);
		DataOutputStream fileOutputStream = new DataOutputStream(new FileOutputStream(file1));
		Random random = new Random();
		int count = 100000;
		for (int i = 0; i < count; i++) {
			int n = random.nextInt();
//			System.out.println(n);
			fileWriter.write(n + "\n");
			fileOutputStream.writeInt(n);
		}
		fileWriter.flush();
		fileWriter.close();
		fileOutputStream.flush();
		fileOutputStream.close();
//		FileReader fileReader = new FileReader(file);
//		int data = -1;
//		while((data = fileReader.read())!=-1)
//		{
//			System.out.println(data);
//		}
//		fileReader.close();
//		Scanner scanner = new Scanner(file);
//		while(scanner.hasNext())
//		{
//			String str = scanner.nextLine();
//			System.out.println(str);
//		}
//		scanner.close();
		DataInputStream dataInputStream = new DataInputStream(new FileInputStream(file1));
		for(int i=0; i<count; i++)
		{
			dataInputStream.readInt();
		}
		dataInputStream.close();
	}
}
